package com.eco.fanliapp.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eco.fanliapp.common.constant.ApiEnum;
import com.eco.fanliapp.entity.AppCompany;
import com.eco.fanliapp.taobao.ItemInfoController;
import com.eco.fanliapp.taobao.SessionController;
import com.eco.fanliapp.utils.ApiUtils;
import com.eco.fanliapp.utils.JsonUtils;
import com.eco.fanliapp.utils.RegUtils;
import com.eco.fanliapp.vo.HaoDanKuItemInfo;
import com.eco.fanliapp.vo.TbkItemInfoGetInfo;
import com.eco.fanliapp.vo.TbkItemInfoGetInfo.NTbkItem;
import com.eco.fanliapp.vo.TbkPrivilegeGetInfo;
import com.eco.fanliapp.vo.TbkPrivilegeGetInfo.TbkPrivilege;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.taobao.api.response.TbkPrivilegeGetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FindItemIdUtils {

    @Autowired
    private IUserCashschemeService iUserCashschemeService;
    @Autowired
    private ItemInfoController itemInfoController;
    @Autowired
    private SessionController sessionController;

    /**
     * 根据ID进行高佣转链，这个地方的主要用于查询当前商品的佣金比例和优惠券信息
     */
    public TbkPrivilege getPrivilegeItemId(Long ItemId) {
        // TODO Auto-generated method getPrivilegeItemId
        StringBuilder builder = new StringBuilder();
        AppCompany com = iUserCashschemeService.findCompanyAll();
        builder.append(ItemId);
        builder.append("/").append(com.getGeneralizePid().split("_")[2]);
        builder.append("/").append(com.getGeneralizePid().split("_")[3]);
        builder.append("/").append(com.getTaoBaoScheme().getAppSession());
//		String str = ApiUtils.getEcoData(ApiEnum.PRIVILEGE_ITEMID_SITELD_ADZONEID_SESSION, builder.toString());
        String str = "";
        TbkPrivilegeGetResponse tbkPrivilegeGetResponse = sessionController.tbkPrivilegeGetResponse(ItemId, Long.parseLong(com.getGeneralizePid().split("_")[2]), Long.parseLong(com.getGeneralizePid().split("_")[3]), com.getTaoBaoScheme().getAppSession());
        if (tbkPrivilegeGetResponse != null) {
            str = JSON.toJSONString(tbkPrivilegeGetResponse);
        }

        TbkPrivilegeGetInfo info = JsonUtils.getJsonToBean(str, TbkPrivilegeGetInfo.class);
        if (info != null && info.getResult() != null) {
            String coupon = RegUtils.getreGex(info.getResult().getData().getCouponInfo() == null ? "减0元" : info.getResult().getData().getCouponInfo(), "(?<=减)[0-9]*(?=元)");
            if (null == coupon || "0".equals(coupon)) {
                info.getResult().getData().setCouponInfo("0");
                return info.getResult().getData();
            } else {
                info.getResult().getData().setCouponInfo(coupon);
                return info.getResult().getData();
            }
        } else {
            return null;
        }
    }

    /**
     * 淘宝获取商品详情（简版）
     */
    public List<NTbkItem> getTaoBaoItemId(String ItemId) {
        // TODO Auto-generated method SearchItemId
//		String str = ApiUtils.getEcoData(ApiEnum.TAOBAO_ITEMID_ITEMDETAIL, "itemId="+ItemId);
        String str = null;
        TbkItemInfoGetResponse tbkItemInfoGetResponse = itemInfoController.itemInfoByItemId(ItemId);
        if (tbkItemInfoGetResponse != null) {
            str = JSON.toJSONString(tbkItemInfoGetResponse);
        }
        TbkItemInfoGetInfo info = JsonUtils.getJsonToBean(str, TbkItemInfoGetInfo.class);
        if (info.isSuccess()) {
            return info.getResults();
        } else {
            return null;
        }
    }

    public static void main(String args[]) {
        FindItemIdUtils findItemIdUtils = new FindItemIdUtils();
        findItemIdUtils.getHaoDanKuItemId("591947735696");

    }

    /**
     * 好单库 获取单品详情
     */
    public HaoDanKuItemInfo getHaoDanKuItemId(String ItemId) {
        // TODO Auto-generated method getHdkSearchItemIdDetail
        String str = ApiUtils.getData(ApiEnum.ITEM_DETAIL, "itemid/" + ItemId + "/");
        String code = JSONObject.parseObject(str).getString("code");
        if ("0".equals(code)) {
            return null;
        } else {
            HaoDanKuItemInfo info = JsonUtils.getJsonToBean(str, HaoDanKuItemInfo.class);
            if (info != null && info.getCode() == 1 && info.getData() != null) {
                return info;
            } else {
                return null;
            }
        }

    }

}
